package com.sun.lib.helper;
/*
 *
 * #                                                   #
 * #                       _oo0oo_                     #
 * #                      o8888888o                    #
 * #                      88" . "88                    #
 * #                      (| -_- |)                    #
 * #                      0\  =  /0                    #
 * #                    ___/`---'\___                  #
 * #                  .' \\|     |# '.                 #
 * #                 / \\|||  :  |||# \                #
 * #                / _||||| -:- |||||- \              #
 * #               |   | \\\  -  #/ |   |              #
 * #               | \_|  ''\---/''  |_/ |             #
 * #               \  .-\__  '-'  ___/-. /             #
 * #             ___'. .'  /--.--\  `. .'___           #
 * #          ."" '<  `.___\_<|>_/___.' >' "".         #
 * #         | | :  `- \`.;`\ _ /`;.`/ - ` : | |       #
 * #         \  \ `_.   \_ __\ /__ _/   .-` /  /       #
 * #     =====`-.____`.___ \_____/___.-`___.-'=====    #
 * #                       `=---='                     #
 * #     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~   #
 * #                                                   #
 * #               佛祖保佑         永无BUG              #
 * #                                                   #
 */

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.ViewGroup;

import com.sun.lib.SBaseHelper;

/**
 * Created by ESSC07 on 2021/12/22.
 */
public class SGroupHelper<T extends ViewGroup> extends SBaseHelper<T> {
    private RectF rectf = new RectF();

    public SGroupHelper(Context context, T view, AttributeSet attrs) {
        super(context, view, attrs);
    }

    public void dispatchDrawBefore(Canvas canvas) {
        int outHeight = (int) (rectf.bottom - rectf.top);
        if (mView.getChildAt(0) != null) {
            Path path = new Path();
            if (mCornerRadius > outHeight * 0.5f) {
                path.addRoundRect(rectf, outHeight / 2, outHeight / 2, Path.Direction.CW);
            } else {
                path.addRoundRect(rectf, copyRaii(mBorderRadii, -mBorderWidth * 0.5f), Path.Direction.CCW);
            }
            // 1. 通过裁剪方式
            canvas.clipPath(path);
        }
    }

    public void dispatchDrawAfter(Canvas canvas) {
        // mClipHelper.dispatchDraw(canvas);
    }

    @Override
    protected void setPadding() {
        super.setPadding();
        calculateRectF();
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        calculateRectF();
    }

    private void calculateRectF() {
        if (rectf == null) rectf = new RectF();
        rectf.left = leftPadding;
        rectf.right = mView.getWidth() - rightPadding;
        rectf.top = topPadding;
        rectf.bottom = mView.getHeight() - bottomPadding;
    }
}
